<?php
class SettleAction extends MbaseAction {
	public $tbname = 'Settle';
	public $obj = '';
	public $mpk = 'settle_id';
	public function index() {
		$this->obj->settling();
		parent :: index();

	}
	public function view() {
		$settle_id = isset ($_GET['settle_id']) ? $_GET['settle_id'] : 0;
		if (!$settle_id)
			return;
		$mu = M('Member');
		$muc = M('Member_content');
		$objmap['settle_id'] = $settle_id;
		$datainfo = $this->obj->relation(true)->where($objmap)->find();
		if ($datainfo) {
			foreach ($datainfo['divide'] as $ukey => $uo) {
				$map['member_id'] = $uo['dmember_id'];
				$baseinfo = $mu->where($map)->field('username,pay_type,pay_card')->find();
				$datainfo['divide'][$ukey]['username'] = $baseinfo['username'];
				$datainfo['divide'][$ukey]['pay_type'] = $baseinfo['pay_type'];
				$datainfo['divide'][$ukey]['pay_card'] = $baseinfo['pay_card'];

				$otherinfo = $muc->where($map)->field('realname,tel,mobile')->find();
				$datainfo['divide'][$ukey]['realname'] = $otherinfo['realname'];
				//if($otherinfo['tel'] != '') $link = $otherinfo['tel'];
				//if($link != '' && $otherinfo['mobile']!='') $link .='/'.$otherinfo['mobile'];
				$datainfo['divide'][$ukey]['tel'] = $otherinfo['tel'] . '-' . $otherinfo['mobile'];
			}
		}
		$this->assign('datainfo', $datainfo);
		$this->display();
	}

	public function settled() {
		$mpk = isset ($_POST['settle_id']) ? $_POST['settle_id'] : '';
		//$divide_id = isset ($_POST['divide_id']) ? $_POST['divide_id'] : '';
		if ($mpk == '')
			$this->error('参数错误', 1, '');
		$map['settle_id'] = $mpk;
		$rs = $this->obj->where($map)->setField(array (
				'is_settled',
				'settled'
			), array (
				1,
				time()
			));
		if ($rs) {
			$mdiv = M('Divide');
			$dmap['settle_id'] = $mpk;
			$mdiv->where($dmap)->setField(array (
				'is_settled',
				'settled'
			), array (
				1,
				time()
			));
			$this->setForward(U('view?settle_id=' . $mpk));
			$this->success('结算成功', 1, '');
		} else {
			$this->error('参数错误', 1, '');
		}
	}
	public function divide(){
		
	}
	public function down_xls() {
		$settle_id = isset ($_GET['settle_id']) ? $_GET['settle_id'] : 0;
		if (!$settle_id)
			return;
		$mu = M('Member');
		$muc = M('Member_content');
		$objmap['settle_id'] = $settle_id;
		$datainfo = $this->obj->relation(true)->where($objmap)->find();
		$setting = D('Set');
		$smap['name'] = 'pay_type';
		$setlist = $setting->where($smap)->relation(true)->find();

		$pay_types = array ();
		foreach ($setlist['list'] as $lvo) {
			$pay_types[$lvo['value']] = $lvo['label'];
		}

		$xlsdata = array ();
		if ($datainfo) {
			foreach ($datainfo['divide'] as $ukey => $uo) {
				$map['member_id'] = $uo['dmember_id'];
				$xlsdata[$ukey] = array ();
				$baseinfo = $mu->where($map)->field('username,pay_type,pay_card')->find();
				$xlsdata[$ukey]['id'] = $uo['dmember_id'];
				$xlsdata[$ukey]['username'] = $baseinfo['username'];
				$xlsdata[$ukey]['pay_type'] = $pay_types[$baseinfo['pay_type']];
				$xlsdata[$ukey]['pay_card'] = $baseinfo['pay_card'];

				$otherinfo = $muc->where($map)->field('realname,tel,mobile')->find();
				$xlsdata[$ukey]['realname'] = $otherinfo['realname'];
				//if($otherinfo['tel'] != '') $link = $otherinfo['tel'];
				//if($link != '' && $otherinfo['mobile']!='') $link .='/'.$otherinfo['mobile'];
				$xlsdata[$ukey]['tel'] = $otherinfo['tel'] . '-' . $otherinfo['mobile'];
				$xlsdata[$ukey]['fee'] = $uo['fee'];
			}
		}

		$keynames = array (
			'id' => '会员编号',
			'username' => '会员名',
			'realname' => '真实姓名',
			'tel' => '联系电话/手机',
			'pay_type' => '结算方式',
			'pay_card' => '结算帐号',
			'commission_fee' => '金额'
		);
		$name = $datainfo['year'] . '-' . $datainfo['month'] . '-commission';
		$xls[] = "<html><meta http-equiv=content-type content=\"text/html; charset=UTF-8\"><body><table border='1'>";
		$xls[] = "<tr><td>ID</td><td>" . implode("</td><td>", array_values($keynames)) . '</td></tr>';
		$index = 0;
		foreach ($xlsdata As $o) {
			$line = array (
				++ $index
			);
			foreach ($keynames AS $k => $v) {
				$line[] = $o[$k];
			}
			$xls[] = '<tr><td>' . implode("</td><td>", $line) . '</td></tr>';
		}
		$xls[] = '</table></body></html>';
		$xls = join("\r\n", $xls);
		header('Content-Disposition: attachment; filename="' . $name . '.xls"');
		die(mb_convert_encoding($xls, 'UTF-8', 'UTF-8'));
	}
}
?>